package com.example.testproject.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View

/**
 *
 * @ClassName: TestMeasureView
 * @Author: android 超级兵
 * @CreateDate: 4/8/22$ 10:32 AM$
 * TODO
 */
class TestMeasureView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {



    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)

        val tempWidth = startMeasure(widthMode,widthMeasureSpec,State.WIDTH)
       val tempHeight = startMeasure(heightMode,heightMeasureSpec,State.HEIGHT)

        setMeasuredDimension(tempWidth,tempHeight)
    }

     private fun startMeasure(mode:Int,measureSpec:Int,state:State) = let{
        var size = MeasureSpec.getSize(measureSpec)
        when(mode){
            MeasureSpec.UNSPECIFIED->{
                // 未指明大小
                Log.i("szjMeasure UNSPECIFIED","size:$size\tstate:${state.name}")
            }
            MeasureSpec.EXACTLY->{
                // 确切大小 match_parent 和 具体值
                Log.i("szjMeasure EXACTLY","size:$size\tstate:${state.name}")
            }
            MeasureSpec.AT_MOST->{
                // 大多数 wrap_content
                size = 400
                Log.i("szjMeasure AT_MOST","size:$size\tstate:${state.name}")
            }
        }
        size
    }


    enum class State{
        WIDTH,
        HEIGHT
    }
}

